#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>


typedef struct _size_case {
    unsigned int size_dec_1;
    unsigned int size;
    unsigned int size_inc_1;
}size_case_t;

#define POW_2(_num) (1 << (_num))

#define CASE_NUM (sizeof(case_list) / sizeof(size_case_t))

#define CASE_ID(_id)    { POW_2(_id) - 1, POW_2(_id), POW_2(_id) + 1 } 

size_case_t case_list[] = {
    CASE_ID(0),
    CASE_ID(1),
    CASE_ID(2),
    CASE_ID(3),
    CASE_ID(4),
    CASE_ID(5),
    CASE_ID(6),
    CASE_ID(7),
    CASE_ID(8),
    CASE_ID(9),
    CASE_ID(10),
    CASE_ID(11),
    CASE_ID(12),
    CASE_ID(13),
};


void brk_test()
{
    void* init_brk = sbrk(0);
    brk(init_brk);
    printf("init brk %08x\n", init_brk);

    for (int i = 1; i < CASE_NUM; i++) {
        int ret;
        void *ret_void;
        size_case_t *size_case = case_list + i;
        printf("sbrk size %d\n", size_case->size_dec_1);
        ret_void = sbrk(size_case->size_dec_1);
        printf("sbrk 0x%x\n", ret_void);
        void* brk1 = sbrk(0);
        ret = brk(init_brk);
        printf("brk  %d\n", ret);

        sbrk(size_case->size);
        void* brk2 = sbrk(0);
        ret = brk(init_brk);
        // printf("%d\n", ret);

        sbrk(size_case->size_inc_1);
        void* brk3 = sbrk(0);
        ret = brk(init_brk);
        // printf("%d\n", ret);

        // printf("%d\n", i);
        printf("2^%d init_brk 0x%08x size -1 0x%08x size  0x%08x size +1 0x%08x\n", 
                i, init_brk, brk1, brk2, brk2);
                
    }

}

int main()
{

    brk_test();
}